package com.sun.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.dto.TypeAndAmount;
import com.sun.pojo.Blog;
import com.sun.pojo.Type;
import com.sun.service.BlogService;
import com.sun.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author: glorysun
 * @date: 2020/8/8
 */
@Controller
public class TypeShowController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private BlogService blogService;

    @GetMapping("/types/{id}")
    public String showType(@PathVariable("id") Long id, Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        List<TypeAndAmount> types=typeService.selectTypeAndAmount();
        if (id == -1) {
            id = types.get(0).getTypeId();
        }
        PageHelper.startPage(pageNum, 3);
        List<Blog> blogByType = blogService.selectBlogByType(id);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogByType);
        model.addAttribute("types",types);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("typeId",id);
        return "types";
    }
}
